package com.unisyou.utils;


import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

public class SignDecode {
	private static SignDecode single;

	private static final String desPassword = "dOi6gPls70";
	private static final String md5Password = "AgwLDS4ZQx";
	
	private SignDecode() {
	}

	public static SignDecode getInstance() {
		if (single == null) {
			single = new SignDecode();
		}
		return single;
	}

	/** 加密 */
	public Map<String,Object> encode(String data) {
		Map<String, Object> resultMap = new HashMap<>();
		DESUtil desUtil = new DESUtil(desPassword);
		String encryptData = desUtil.encode(data);
		String nonce = UUID.randomUUID().toString().replace("-", "");
		StringBuffer sb = new StringBuffer(md5Password).append(nonce).append(encryptData);
		String signature = Md5.init().strToMd5(sb.toString());
		resultMap.put("encryptData", encryptData);
		resultMap.put("signature", signature);
		resultMap.put("nonce", nonce);
		return resultMap;
	}

	/**
	 * 解密
	 * @param encryptData 需要的解密字符串
	 * @param signature(加密后的的数据串 + 随机串)  签名字符串
	 * @param nonce 随机串
	 * @return
	 */
	public String decode(String encryptData, String signature, String nonce) {
		String result = null;
		if(StringUtils.isEmpty(nonce) || StringUtils.isEmpty(encryptData) || StringUtils.isEmpty(signature)){
			return null;
		}
		StringBuffer sb = new StringBuffer(md5Password).append(nonce).append(encryptData);
		String signature2 = Md5.init().strToMd5(sb.toString());
		if (signature.equals(signature2)) {
			DESUtil desUtil = new DESUtil(desPassword);
			result = desUtil.decode(encryptData);
		}
		return result;
	}
	public static void main(String[] args) {
		String str = "7cbc3e85a492031caa8b3d0ac90a57c41a9396c75418482f168be8a0ce3b9e2ff1a58d1eaf854ad2fcc80140e8027b5b53a03a800c7aa273e44caaa121372937bfe81c98ee8708e187fa9d819925ab3ee44c907e919eda938f4869d39d4b001d26b82430a8f7731496d2efbcf9c0e7abac4d3adb93c092c1c476dc077e6058a345a0ffa47e8e89412d2088f73a29bc1cd341212df016fd822101ee5c54ed3243672fea346c02d9031a571859a80375ddd50c97522cf38e846bdc8b401b03879e90413b3fd7963684aeb5d704b5a25b885ecf81b4903d43013c4469fee9c05d205b2c26a906f8e6635f71d9b6c37a7e033fb2d7e7213858c067cbce43918eb769b2272242e2c61ffb726139a6f59d2cb69d92dcb932f8907150c45840dc0a6d743ab6bfe8b6b45db6e4dbe2a577e7b84c87fa9d819925ab3ee44c907e919eda932fd21ba4d06ddf3e31e95f6f6d00b7a2a0be59b328b49695c66635008e00a42fd9a8b8b72efc51577487a362958b3be8bbce1147694e2355ad9fc99d0dca5648e050129e9d24d5df98e8500706c03e0630dd7e3615cd8e3eea9baf543d62e2e4f2b17063622cfc52c8947313dce65f16b1123a880566ff6150a089ba408cbed5c322e09c7afcfe9bf7c4d1fcc873599c5639bd718fce6312b7a7ad906362013267faf24116ea76ad563ac6d90a989c08e6fd6be407ccceb90cc62a79889ec8e3ed1e482627d0a725ef3a20e6c8864ccf05e4f503e90fd574e661e348520df0988d337ab7c688b686527c076fa83ab19286b8d467431dae4a3b9f2f441df7011b55b27fb63ccb623cbfe81c98ee8708e1d3e4fac56d76a787dab128e56758af8b66b0bef372a9a00ddfe576dbc4ee239b7ce8ca608c4a3c7e6c537e9b7dbda205439b346a4e596991c781d4485b3d5fbe4967aad02c8b3c685edcd933d7866d759a5a92e13d43a3f4c75665c5e735621d966c4e7d75e9af517d9a39d49059b214c630f3ec862eb6e54f7562ba6a290948";
		String nonce = "3007b1d55e3c42b0b49cac1815231981";
		String signature = "7ea0224e4cd2ea1cf7157fa1d18d7d12";
		System.out.println(SignDecode.getInstance().decode(str, signature, nonce));
	}
}
